package com.map;

import java.util.HashMap;
import java.util.Map;

public class MapTest {
	
		public static void main(String[] args) {
			Map<Integer, String> map = new HashMap<Integer, String>();
			//加入数据key-value
			map.put(1, "张三");
			map.put(2, "李四");
			map.put(3, "王五");
			System.out.println(map);//key不重复的，value可以重复的
			map.put(1, "张三丰");
			map.put(4, "王五");
			System.out.println(map);
			map.put(null,"张三");
			System.out.println(map);//key可以为空，只能有一个
			map.put(null,"王五");
			map.put(1,null);
			System.out.println(map);
			map.put(null,null);//value可以为空，可以有多个
			System.out.println(map);
			
			/*
			 * 清空key-value
			 */
			//map.clear();
			//System.out.println(map);
			System.out.println("map.containsKey(1)"+map.containsKey(1));
			System.out.println("map.containsKey(-1)"+map.containsKey(-1));
			System.out.println("map.containsKey(\"李四\"):"+map.containsKey("李四"));
			System.out.println("map.containsKey(\"张三\"):"+map.containsKey("张三"));
		
			//根据key获取value
			System.out.println("map.get(2)="+map.get(2));
			System.out.println("map.get(-1)="+map.get(-1));
			System.out.println("map.getOrDefault(2,\"默认值\")="+map.getOrDefault(2, "默认值"));
			System.out.println("map.getOrDefault(-1,\"默认值\")="+map.getOrDefault(-1, "默认值"));
			
			//是否为空
			System.out.println("map.isEmpty="+map.isEmpty());
			
			//数量
			System.out.println("map.size="+map.size());
			
			//删除键值队key-value
			System.out.println("map.remove(4)"+map.remove(4));
			System.out.println(map);
			System.out.println("map.remove(1, \"张三丰\")"+map.remove(1, "张三丰"));
			System.out.println("map.remove(2, \"李四\"))"+map.remove(2, "李四"));
			System.out.println("map.remove(3, \"王五\"))"+map.remove(3, "王五"));
			System.out.println(map);
			//
		}
}
